package com.hh.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.entity.UserCollection;
import com.hh.es.ProdEs;
import com.hh.feign.UserCollectionProdFeign;
import com.hh.mapper.UserCollectionMapper;
import com.hh.service.UserCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 辉辉
 * @since 2022-08-19
 */
@Service
@Slf4j
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {

    @Resource
    private UserCollectionMapper userCollectionMapper;

    @Resource
    private UserCollectionProdFeign userCollectionProdFeign;

    /**
     * 分页查询用户的商品收藏
     *
     * @param page
     * @param openId
     * @return
     */
    @Override
    public IPage<ProdEs> findUserCollectionProd(Page<UserCollection> page, String openId) {
        // 分页查询收藏表
        IPage<UserCollection> collectionIPage = userCollectionMapper.selectPage(page, Wrappers.<UserCollection>lambdaQuery()
                .eq(UserCollection::getUserId, openId)
        );
        List<UserCollection> userCollections = collectionIPage.getRecords();
        IPage<ProdEs> prodEsIPage = new Page<>(page.getCurrent(), page.getSize());
        if (CollectionUtils.isEmpty(userCollections)) {
            // 因为page源码内进行此操作了 所以不需要设置空集合
//            prodEsIPage.setRecords(Collections.emptyList());
            return prodEsIPage;
        }
        // 拿到收藏表的集合 但是并没有商品信息
        List<Long> prodIds = userCollections.stream()
                .map(UserCollection::getProdId)
                .collect(Collectors.toList());
        // 查询es 远程调用查询
        List<ProdEs> prodEsList = userCollectionProdFeign.findProdEsByIds(prodIds);
        prodEsIPage.setRecords(prodEsList);
        return prodEsIPage;
    }

    /**
     * 查询用户是否收藏该商品
     *
     * @param prodId
     * @param openId
     * @return
     */
    @Override
    public Boolean findUserIsCollection(Long prodId, String openId) {
        Integer count = userCollectionMapper.selectCount(Wrappers.<UserCollection>lambdaQuery()
                .eq(UserCollection::getUserId, openId)
                .eq(UserCollection::getProdId, prodId)
        );
        return count > 0;
    }

    /**
     * 用户收藏或者取消商品收藏
     *
     * @param prodId
     * @param openId
     * @return
     */
    @Override
    public void addOrCancelCollection(Long prodId, String openId) {
        // 查询这个商品之前有没有被这个用户收藏过
        UserCollection userCollection = userCollectionMapper.selectOne(Wrappers.<UserCollection>lambdaQuery()
                .eq(UserCollection::getUserId, openId)
                .eq(UserCollection::getProdId, prodId)
        );
        // 如果没有 就收藏
        if (ObjectUtils.isEmpty(userCollection)) {
            UserCollection newCollection = new UserCollection();
            newCollection.setProdId(prodId);
            newCollection.setUserId(openId);
            newCollection.setCreateTime(new Date());
            // 插入数据库
            userCollectionMapper.insert(newCollection);
            return ;
        }
        // 如果之前有 取消收藏
        // 删掉他
        userCollectionMapper.deleteById(userCollection.getId());
    }
}
